package grpc

import (
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/health"
	"google.golang.org/grpc/health/grpc_health_v1"
	"net"
)

func RegisterGrpc(host string, port int, call func(server *grpc.Server)) {
	// 地址
	addr := fmt.Sprintf("%s:%d", host, port)
	// 1.监听
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		fmt.Printf("监听异常:%s\n", err)
		return
	}
	fmt.Printf("监听端口：%s\n", addr)
	// 2.实例化gRPC
	s := grpc.NewServer()
	call(s)
	grpc_health_v1.RegisterHealthServer(s, health.NewServer())
	// 4.启动服务端
	s.Serve(listener)
}
